# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=ngs
pkgver=0.2.14
pkgrel=0
pkgdesc="Next Generation Shell"
url="https://ngs-lang.org"
# armhf, armv7, x86: tests fail
arch="all !armhf !armv7 !x86"
license="GPL-3.0"
makedepends="
	bash
	cmake
	gc-dev
	json-c-dev
	libffi-dev
	pcre-dev
	peg
	samurai
	"
subpackages="
	$pkgname-aws::noarch
	$pkgname-vim::noarch
	"
source="https://github.com/ngs-lang/ngs/archive/v$pkgver/ngs-$pkgver.tar.gz
	cmakelists.patch
	busybox-compat.patch
	"

build() {
	local crossopts=
	if [ "$CBUILD" != "$CHOST" ]; then
		crossopts="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi

	# The test segfaults with the default stack size.
	LDFLAGS="$LDFLAGS -Wl,-z,stack-size=2097152" \
	cmake -B build -G Ninja \
		-DCMAKE_BUILD_TYPE=MinSizeRel \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		$crossopts
	cmake --build build
}

check() {
	cd build
	CTEST_OUTPUT_ON_FAILURE=TRUE NGS_PATH="$builddir/lib" ctest
}

package() {
	DESTDIR="$pkgdir" cmake --install build

	# stdlib.ngs is bundled into the ngs binary, so it's not used.
	cd "$pkgdir"/usr/lib/ngs
	rm -rf experiments lang-tests.ngs stdlib.ngs
}

aws() {
	pkgdesc="$pkgdesc - experimental AWS library"
	depends="$pkgname=$pkgver-r$pkgrel"

	amove usr/lib/ngs/autoload/globals/AWS*
}

vim() {
	pkgdesc="$pkgdesc (vim syntax)"
	depends=""
	install_if="vim $pkgname=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"/usr/share/vim/vimfiles
	cp -r "$builddir"/vim/* "$subpkgdir"/usr/share/vim/vimfiles/
}

sha512sums="
5b0f8fc94eb936133312d088cabd09f3097dfc0157fafaab7b25279b1ce85a1359de4df2b1499d287e64b2e98d6fc9dd02ef12bd5e7a672834b361f6d7f4bb8d  ngs-0.2.14.tar.gz
b53ff460f4e5ccf3db8819c897dbfce5b97cec8c6b2b8f917479c9aa3e8caea923a3d9b6cb73f8baa79926f8463c2eac23c89b72163220ee898ce055a132091b  cmakelists.patch
a3c22590c2ba072eab773859d9fa50c55d88f8f7af5d70c0ddb4bd005d69a549f87625d5da47f515256ef004bdae2f2366e427c63b16a4f9471eca3effdec0cd  busybox-compat.patch
"
